class IUP_COLOR_DIALOG
-- Creates the Color Dialog element. It is a predefined dialog for selecting a 
-- color.
--
-- There are 3 versions of the dialog. One for Windows only, one for GTK only 
-- and one for all systems, but it is based on the IUP_COLOR_BROWSER control 
-- that depends on the CD library.
--
-- The Windows and GTK dialogs can be shown only with the popup function. The 
-- IUP_COLOR_BROWSER based dialog is a IUP_DIALOG that can be shown as any 
-- regular IUP_DIALOG.
--
-- IMPORTANT: The IUP_COLOR_BROWSER based dialog is included in the Controls 
-- Library. When the Controls Library is initialized the Windows and GTK 
-- dialogs are not available anymore, i.e. before the Controls Library 
-- initialization only the Windows and GTK dialogs are available, after only 
-- the IUP_COLOR_BROWSER based dialog is available.

inherit
	IUP_WIDGET
		redefine
			execute_colorupdate,
			execute_help
		end
	IUP_WIDGET_INTERNALS
	IUP_WIDGET_TITLE
	IUP_WIDGET_POPUP
	IUP_WIDGET_ICON
	IUP_WIDGET_PARENT_DIALOG

create {ANY}
  color_dialog
	
feature {ANY}

	color_dialog
		local
			a_color_dialog: POINTER
		do
			a_color_dialog := int_color_dialog
			set_widget(a_color_dialog)
		end

	-- Attributes

	set_alpha (value: INTEGER)
		-- [ColorBrowser and GTK only]: if defined it will enable an alpha 
		-- selection additional controls with its initial value. If the user 
		-- pressed the Ok button contains the returned value. Default: no 
		-- defined, or 255 if SHOWALPHA=True.
		require
			value >= 0
			value <= 255
		do
			iup_open.set_attribute(Current, "ALPHA", value.out)
		end

	set_color_table (colors: STRING)
		-- List of colors separated by ";". In GTK and in the ColorBrowser based 
		-- accepts 20 values and if not present the palette will not be visible. 
		-- In Windows accepts 16 values and will be always visible, even if the 
		-- colors are not defined (in this case are initialized with black). If a 
		-- color is not specified then the default color is used. You can skip 
		-- colors using ";;".
		do
			iup_open.set_attribute(Current, "COLORTABLE", colors)
		end

	get_color_table: STRING
		do
			Result := iup_open.get_attribute(Current, "COLORTABLE")
		end

	set_show_alpha (state: BOOLEAN)
		-- [ColorBrowser and GTK only]: if enabled will display the alpha 
		-- selection controls, regardless if ALPHA is defined for the initial 
		-- value or not. 
		do
			iup_open.set_attribute(Current, "SHOWALPHA", boolean_to_yesno(state))
		end

	set_show_color_table (state: BOOLEAN)
		-- If enabled will display the color table, regardless if COLORTABLE is 
		-- defined or not. The default colors in the color table are different in 
		-- GTK and in the ColorBrowser based dialog. In Windows the default 
		-- colors are all black.
		do
			iup_open.set_attribute(Current, "SHOWCOLORTABLE", boolean_to_yesno(state))
		end

	set_show_hexadecimal (state: BOOLEAN)
		-- [ColorBrowser only]: if enabled will display the Hexadecimal notation 
		-- of the color.
		do
			iup_open.set_attribute(Current, "SHOWHEX", boolean_to_yesno(state))
		end

	set_show_help (state: BOOLEAN)
		-- [ColorBrowser only]: if enabled will display the Help button. In GTK 
		-- and Windows, the Help button is shown only if the HELP_CB callback is 
		-- defined.	
		do
			iup_open.set_attribute(Current, "SHOWHELP", boolean_to_yesno(state))
		end

	get_status: INTEGER
		-- (read-only): Return "1" if the user pressed the Ok button, "0" if 
		-- pressed the Cancel button or close the dialog.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "STATUS")

			if not str.is_empty then
				Result := 1
			else
				Result := 0
			end
		end

	set_value (red, green, blue, alpha: INTEGER)
		-- The color value in RGB coordinates and optionally alpha. It is used as 
		-- the initial value and contains the selected value if the user pressed 
		-- the Ok button. Format: "R G B A". Each component range from 0 to 255.
		do
			iup_open.set_attribute(Current, "VALUE", rgba_to_string(red,
																					  green,
																					  blue,
																					  alpha))
		end

	get_value: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		-- Return the selected value if the user pressed the Ok button. 
		-- Format: "R G B A". Each component range from 0 to 255.
		do
			Result := iup_open.get_rgba(Current, "VALUE")
		end

	set_value_hsi (h, s, i: INTEGER)
		-- [ColorBrowser only]: The color value in HSI coordinates. It is used as 
		-- the Format: "H S I". Each component range from 0-359, 0-100 and 0-100 
		-- respectively.	
		do
			iup_open.set_attribute(Current, "VALUEHSI", hsi_to_string(h, s, i))
		end

	get_value_hsi: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the selected value if the user pressed the Ok button. Format: 
		-- "H S I". Each component range from 0-359, 0-100 and 0-100 respectively.
		do
			Result := iup_open.get_hsi(Current, "VALUEHSI")
		end

	set_value_hexadecimal (color: STRING)
		-- [ColorBrowser only]: The color value in RGB Hexadecimal notation. It 
		-- is used as the initial value. Format: "#RRGGBB". Each component range 
		-- from 0-255, but in hexadecimal notation.
		do
			iup_open.set_attribute(Current, "VALUEHEX", color)
		end

	get_value_hexadecimal: STRING
		-- Return the selected value if the user pressed the Ok button. Format: 
		-- "#RRGGBB". Each component range from 0-255, but in hexadecimal 
		-- notation.	
		do
			Result := iup_open.get_attribute(Current, "VALUEHEX")
		end

	-- Callbacks

	set_cb_help (act: detachable PROCEDURE[TUPLE[IUP_COLOR_DIALOG]])
		-- Action generated when the user press F1 at a control. In Motif 
		-- is also activated by the Help button in some workstations 
		-- keyboard.
		-- Returns: IUP_CLOSE will be processed.
		local
			operation: INTEGER
		do
			cb_help := act

			if cb_help /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "HELP_CB", "NONEEDED", operation)
		end

	set_cb_color_update (act: detachable FUNCTION[TUPLE[IUP_COLOR_DIALOG], STRING])
		-- [ColorBrowser only]: Action generated when the color is updated 
		-- in the dialog. It is also called when the color is updated 
		-- programmatically.	
		local
			operation: INTEGER
		do
			cb_colorupdate := act

			if cb_colorupdate /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "COLORUPDATE_CB",
										  "NONEEDED", operation)
		end

feature {IUP}

	execute_help
		do
			if attached cb_help as int_cb then
				int_cb.call([Current])
			end
		end

	execute_colorupdate: STRING
		do
			if attached cb_colorupdate as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	-- For callbacks

	cb_help: detachable PROCEDURE[TUPLE[IUP_COLOR_DIALOG]]

	cb_colorupdate: detachable FUNCTION[TUPLE[IUP_COLOR_DIALOG], STRING]

	-- Internal

	int_color_dialog: POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupColorDlg();"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
